<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="">
      <el-form-item label="主单/库位/托盘码" prop="job_number">
        <el-input
          v-model="queryParams.key"
          placeholder="主单/库位/托盘码"
          clearable
          size="small"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button v-if="multipleSelection.length>0" type="danger" size="mini" @click="handleDel2">批量清除锁定</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column prop="warehouse_out_id" label="出库主单" align="center" />
      <el-table-column prop="config_name" label="库位" align="center" />
      <el-table-column prop="tray_id" label="托盘码" align="center" />
      <el-table-column prop="box_id" label="箱号" align="center" />
      <el-table-column prop="slice_id" label="片号" align="center" />
      <el-table-column prop="number" label="锁定数量" align="center" />
      <el-table-column prop="username" label="拣货人" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDel(scope.row)"
          >清除锁定
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
  </div>
</template>
<script>
import { lockList, clearLock } from '@/api/stock'
export default {
  name: 'StorehouseLocking',
  data() {
    return {
      multipleSelection: [],
      // 遮罩层
      loading: true,
      open: false,
      title: '',
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      form: {},
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        key: undefined
      },
      show: false,
      id: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      lockList(this.queryParams).then(response => {
        console.log(response, 'yes')
        if (response.code === 10000) {
          that.list = response.data
          that.total = response.count
          that.loading = false
        }
      })
    },
    /** 批量清除锁定 */
    handleDel2() {
      const that = this
      const list = that.multipleSelection.map(item => item.id)
      that.$confirm('是否要批量清除锁定？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        clearLock({ id: list }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 删除按钮操作 */
    handleDel(row) {
      console.log(row)
      const that = this
      that.$confirm('是否要清除锁定？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        clearLock({ id: [row.id] }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        company_code: undefined,
        name_c: undefined
      }
      this.handleQuery()
    }
  }
}

</script>

